Ext.define('AmpleHarvest.objects.forms.irtCommon',
{
	extend				: 'Ext.form.Panel',
	alias				: 'widget.ampleharvest-objects-forms-irtcommon',
	border				: false,
	autoScroll			: true,
	requestorGUID		: null,
	canEdit				: true,
	canComment			: false,
	requestId			: null,
	requires			:
	[
			"Ext.button.Button", 
			"Ext.form.field.Text", 
			"Ext.form.FieldSet",
			"Ext.form.field.Date", 
			'Ext.form.ComboBox',
			'Ext.layout.container.VBox',
			'Ext.layout.container.HBox',
			'AmpleHarvest.objects.forms.irtCommon.userInformation',
			'AmpleHarvest.objects.forms.irtCommon.classification',
			'AmpleHarvest.objects.forms.irtCommon.requestInformation',
			'AmpleHarvest.objects.forms.irtCommon.requestTicketInformation',
			'AmpleHarvest.eventHandler.cWindowEvents', 
		],
	statics : {},
	
	processSubmitButton: function(){
	
		this.submitDataByPUT(
			this.irtRequestVBox, 
			'Submitting Your Import Request',
			'All fields are required. Please fix the errors indicated and resubmit the form.',
			obj_config.C_STR_EXTERNAL_PATH + '/rest/irt/data.php',
			this.submitSuccess, 
			this.submitFail
		);	
	},
	
	processCommentButton: function(){
		Ext.MessageBox.confirm(
			"Warning: Post Comment",
			"You are about to post a comment to the comments feed.<br>This action cannot be undone.<br><br>Do you wish to continue?",
			this.processPostCommentRequest,
			this
		);
	},
	
	processPostCommentRequest: function(buttonId){
		switch(buttonId){
			case 'yes' :
				this.submitDataByPUT(
					this.irtCommentsVBox,
					'Posting Comment',
					'Text is required in for posting comments. Please add a comment.',
					obj_config.C_STR_EXTERNAL_PATH + '/rest/irt/comments.php',
					this.commentSuccess, 
					this.submitFail
					);
			  break;
			case 'no':
			  break;
			default:
		}
	},
	
	submitDataByPUT: function(formData, incomingWaitMsg, errorMsg, putURL, successFunction, failureFunction){
		var submittedForm = formData.getForm();
		
		if(submittedForm.isValid()){
			submittedForm.submit({
				method: 'PUT',
				url      : putURL,
		        waitMsg  : incomingWaitMsg,
		        success  : successFunction,
		        failure  : failureFunction,
		        scope : this
			});
		} else {
			alert(errorMsg);
		}
	},
	
	processCancelButton: function(){
		Ext.MessageBox.confirm(
			"Warning: Cancel Import Request",
			"All changes will be lost.<br><br>Do you wish to continue?",
			this.processImportCancelRequest,
			this
		);
	},
	
	commentSuccess: function(){
		this.irtCommentArea.reset();
		this.commentsGrid.commentGrid.getStore().load();
	},
	
	submitSuccess : function(){
		alert('Submit Success!');
		m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cMenuEvents.C_STR_EVENT_GO_IMPORT_REQUEST_TOOL);
	},
	
	submitFail : function(str_Function, mix_Arg)
	{
	   if (typeof mix_Arg.result === 'undefined') {
		   console.log(mix_Arg);
		   Ext.MessageBox.alert("Error", mix_Arg.response.status+": "+mix_Arg.response.statusText, close);
		   
	   } else {
		   if(! mix_Arg.result.errors.fatal) {
			   this.ownerCt.close();
			   Ext.MessageBox.alert("Result",
					   "Your Request has been sent.<br><br>"
						   +"The following non-fatal error was encountered:<br>"
						   +mix_Arg.result.errors.msg,
					   close);
		   } else {
			   Ext.MessageBox.alert("Error", mix_Arg.result.errors.msg, close);
		   }
	   }
	},

	processImportCancelRequest : function(buttonId){
		switch(buttonId){
			case 'yes' :
				m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cMenuEvents.C_STR_EVENT_GO_IMPORT_REQUEST_TOOL);
			  break;
			case 'no':
			  break;
			default:
		}
	},
	
	initComponent			: function()
	{
		this.irtTicketVBox = Ext.create('Ext.form.Panel', 
		{ 
			border: false,
			hidden: !this.canComment,
			disabled: !this.canComment,
			layout : 
			{
				type: 'vbox',
				align: 'left'
			}
		});	
		
		this.irtTicketVBox.add(
			this.userInformation = Ext.create("AmpleHarvest.objects.forms.irtCommon.requestTicketInformation", 
				{
					m_str_group_ID : 'F1D8A48E-884C-40B1-8572-40658ACE062A'
				}
			)
		);

		this.irtRequestVBox = Ext.create('Ext.form.Panel', 
		{ 
			border: false,
			layout : 
			{
				type: 'vbox',
				align: 'left'
			}
		});	
	
		this.irtCommonHBox = 
		Ext.create('Ext.Panel', 
			{ 
				border		: false,
				layout 		: 
				{
					type: 'hbox',
					align: 'middle'
				}
			});		
	
		this.irtCommonHBox.add(
			this.userInformation = Ext.create("AmpleHarvest.objects.forms.irtCommon.userInformation", {requestorGUID: this.requestorGUID})
		);
		
		this.irtCommonHBox.add(
				this.classification = Ext.create("AmpleHarvest.objects.forms.irtCommon.classification")
		);
		

		this.cancelButton = Ext.create('Ext.Button', {
		    text: 'Cancel',
		    hidden: !this.canEdit,
            margin: 20,
            padding: 5,
            width: 200,
		    scale: 'medium',
		    handler: function() {
		    	this.up('ampleharvest-objects-forms-irtcommon').processCancelButton();
			}
		});
		
		this.submitButton = Ext.create('Ext.Button', {
		    text: 'Submit',
		    hidden: !this.canEdit,
            margin: 20,
            padding: 5,
            width: 200,
		    scale: 'medium',
		    handler: function() {
		    	this.up('ampleharvest-objects-forms-irtcommon').processSubmitButton();
			}
		});
		
		this.postCommentButton = Ext.create('Ext.Button', {
		    text: 'Post Comment',
		    hidden: !this.canComment,
		    scale: 'medium',
		    handler: function() {
		    	this.up('ampleharvest-objects-forms-irtcommon').processCommentButton();
			}
		});
		
		this.hiddenGUID = Ext.create("Ext.form.field.Hidden", 
			{
		        name: 'id',
		        value: this.requestId,
		        submitValue: true
		    }
		);	
		
		this.hiddenRequestId = Ext.create("Ext.form.field.Hidden", 
			{
		        name: 'requestID',
		        value: this.requestId,
		        submitValue: true
		    }
		);	
				
		this.hiddenCommentDate = Ext.create("Ext.form.field.Hidden", 
			{
		        name: 'comment_date',
		        xtype: 'datefield',
				value: new Date(),
		        submitValue: true
		    }
		);
		
		this.hiddenCommenterId = Ext.create("Ext.form.field.Hidden", 
			{
		        name: 'commenter_id',
				value: obj_config.user.userName,
		        submitValue: true
		    }
		);
		
		this.requestInformation = Ext.create("AmpleHarvest.objects.forms.irtCommon.requestInformation", 
			{
				submitButton	: this.submitButton,
				cancelButton	: this.cancelButton
			}
		);
		
		this.irtRequestVBox.add(
			this.irtTicketVBox,
			this.hiddenGUID,
			this.irtCommonHBox,
			this.requestInformation
		);
		
		//----- COMMENTS SECTION-----//
		this.irtCommentsVBox = Ext.create('Ext.form.Panel',
			{ 
				border: false,
				layout : 
				{
					type: 'vbox',
					align: 'left'
				},
				
				items: [
				        this.hiddenRequestId,
				        this.hiddenCommentDate,
				        this.hiddenCommenterId
				]
			}
		);	
		
		this.irtCommentArea = Ext.create('Ext.form.TextArea',
			{
				name: 'comment',
				hidden: !this.canComment,
			 	width: 1210,
			 	boxMinWidth: 1210,
				minHeight: 100,
				padding: '5,0,5,0'
			}
		);
		
		this.commentsGrid = Ext.create('AmpleHarvest.objects.comments.view.Comments',
			{
				border: false,
				requestId: this.requestId
			}
		);		
		this.irtCommentsFieldSet = Ext.create('Ext.form.FieldSet',
			{
				title : 'Comments',
			 	width: 1240,
			 	boxMinWidth: 1240,
				items: 
					[
						 this.commentsGrid, 
						 this.irtCommentArea, 
						 this.postCommentButton
					]
			}
		);
		this.irtCommentsVBox.add(this.irtCommentsFieldSet);


		this.items =
		[
		 	this.irtRequestVBox,
		 	this.irtCommentsVBox,
		];

        this.callParent();
	}
}
);